// pages/commander/commander.js
var app = getApp();
var commonURL = app.data.baseURL;
var commonURLYY = app.data.newURL;
var commonURLXX = app.data.lastURL;
var page=1;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isTimeOut: false,
    inptUserInfo: {
      name: '',
      mobile: '',
      code: '',
    },
    userInfo: {},
    isShowWithDraw: false,
    requestCode: '',
    timeout: 60,
    codeText: '获取验证码',
    myPromotion: [],
    setting: {},
    orderList: [],
    withdrawMoney: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.requestUserInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this;
    page=1;
    let arr = [];
    let title = ['可提现', '审核中', '已提现'];
    let keys = ['withdrawals', 'commander_apply', 'commander_withdraw'];
    wx.request({
      url: commonURLXX+'op=getcommissioninfo',
      data:{
        uniacid: app.data.GZHid, 
        openid: app.data.openid
      },
      success:function(res){
        console.log(res)
        that.setData({
          area_commander_settled: res.data.info.area_commander_settled ,
          area_commander_apply: res.data.info.area_commander_apply,
          area_commander_withdraw: res.data.info.area_commander_withdraw
        })
      }
    })
    that.setData({
      myPromotion: arr
    })
    wx.request({
      url: commonURLXX +'op=getcommission',
      data:{
        uniacid: app.data.GZHid, 
        areaid:wx.getStorageSync('xiaoqu').id,
         openid:app.data.openid,
         page:1,
         size:'10'
      },
      success:function(res){
        // var money=0;
        // for(var i=0;i<res.data.list.length;i++){
        //     for (var j = 0; j < res.data.list[i].goods.length; j++) {
        //       money += parseInt(res.data.list[i].goods[j].commission)
        //     }       
        // }
        that.setData({
          orderList: res.data.list
          // money: money
        })
      }
    })
  },
  onReachBottom: function () {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    page++;
    wx.request({
      url: commonURLXX + 'op=getcommission',
      data: {
        uniacid: app.data.GZHid,
        areaid: wx.getStorageSync('xiaoqu').id,
        openid: app.data.openid,
        page:page,
        size:'10'
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res)
        // var money = that.data.money;
        // for (var i = 0; i < res.data.list.length; i++) {
        //     for (var j = 0; j < res.data.list[i].goods.length; j++) {
        //       money += parseInt(res.data.list[i].goods[j].commission)
        //     }        
        // }
        that.setData({
          orderList: that.data.orderList.concat(res.data.list)
          // money: money
        })
      }
    })
  },
  onUnload() {
    this.stop();
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  requestMessageCode() {                     //获取手机验证码
    var _this = this;
    var mobile = this.data.inptUserInfo.mobile;
    if (!(/^1[34578]\d{9}$/.test(mobile))) {
      wx.showToast({
        title: '手机号码不正确',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    wx.request({
      url: app.data.baseURL + '&op=code',
      data: { num: 0, mobile, retrieve: 1 },
      success: function (res) {
        console.log(res.data);
        _this.setData({
          requestCode: res.data.code
        })
        _this.timer = setInterval(function () {
          if (_this.data.timeout == 0) {
            _this.setData({
              codeText: '重新获取',
              timeout: 60,
            })
            _this.stop();
          } else {
            _this.data.timeout = _this.data.timeout - 1;
            console.log(_this.data.timeout)
            _this.setData({
              timeout: _this.data.timeout
            })
          }
        }, 1000)
      }
    })
  },
  requestApplication() {                                              //兼职团长
    if (this.data.inptUserInfo.name == '') {
      wx.showToast({
        title: '请填写申请人姓名',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (!(/^1[34578]\d{9}$/.test(this.data.inptUserInfo.mobile))) {
      wx.showToast({
        title: '手机号码不正确',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    if (this.data.setting.smscode != 2) {
      if (this.data.inptUserInfo.code != this.data.requestCode) {
        wx.showToast({
          title: '验证码不正确',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
    }

    let _this = this;
    wx.request({
      url: app.data.XCX_PAY + '&op=apply_commander',
      data: { id: this.data.userInfo.id, openid: app.data.openid, ...this.data.inptUserInfo },
      success: function (res) {
        console.log(res.data);
        if (res.data.status) {
          _this.requestUserInfo()
        }
      }
    })
  },
  requestUserInfo() {                             //获取申请人信息
    var _this = this;
    wx.request({
      url: app.data.XCX_PAY + '&op=commander',
      data: { openid: app.data.openid },
      success: function (res) {
        console.log(res.data)
        if (res.data.status) {
          var obj = {
            id: res.data.msg.id,
            apply: res.data.msg.apply,
            apply_status: res.data.msg.apply_status,
            is_area_commander: res.data.msg.is_area_commander
          }
          Date.prototype.toLocaleString = function () {
            return this.getFullYear() + "/" + (this.getMonth() + 1) + "/" + this.getDate() + "/ " + this.getHours() + ":" + this.getMinutes() + ":" + this.getSeconds();
          }
          res.data.orders.map(item => {
            item.createtime = new Date(item.createtime * 1000).toLocaleString();
          })
          _this.setData({
            userInfo: obj,
            setting: res.data.setting,
            // orderList: res.data.orders
          }, () => {
            if (obj.apply_status == '1') {
              _this.requestMyCashOrder(res.data.msg);
            }
          })
        }
      }
    })
  },
  navTo:function(){
    wx.navigateTo({
      url: '../regimental/regimental'
    })
  },
  requestMyCashOrder(data) {                                              //我的推广
    let _this = this;
    // let arr = [];
    // let title = ['可提现', '审核中', '已提现'];
    // let keys = ['withdrawals', 'commander_apply', 'commander_withdraw']
    // title.map((item, index) => {
    //   arr.push({
    //     title: item,
    //     [keys[index]]: !data[keys[index]] && typeof data[keys[index]] === 'object' ? 0 : data[keys[index]],
    //     price: !data[keys[index]] && typeof data[keys[index]] === 'object' ? 0 : data[keys[index]],
    //   })
    // })
    // _this.setData({
    //   myPromotion: arr
    // })
    // console.log(arr);
  },
  requestApplyWithDrawals() {  
    var that=this;                                       //申请提现
    if (Number(this.data.withdrawMoney) < 1 || Number(this.data.withdrawMoney) > this.data.area_commander_settled) {
      wx.showToast({
        title: '提现金额不正确',
        icon: 'loading',
        duration: 2000
      })
    }
    wx.request({
      url: commonURLXX + 'op=apply_area_commission',
      data: { 
        uniacid: app.data.GZHid,
        openid: app.data.openid,  
        price: this.data.withdrawMoney
       },
      success: function (res) {
        console.log(res.data)
        if (res.data.status==1){
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel:false,
            complete:function(res){
              that.setData({
                isShowWithDraw:false
              })
              that.onShow();
              that.onLoad();
            }
          })
        }
      }
    })
  },
  inputValue(e) {
    switch (e.target.dataset.name) {
      case 'user_name':
        this.setData({ 'inptUserInfo.name': e.detail.value })
        break;
      case 'phone':
        this.setData({ 'inptUserInfo.mobile': e.detail.value })
        break;
      case 'code':
        this.setData({ 'inptUserInfo.code': e.detail.value })
        break;
      default:
        break;
    }
  },
  inputWithdrawMoney(e) {
    this.setData({ withdrawMoney: e.detail.value })
  },
  toggleWithDraw() {
    if (this.data.isShowWithDraw) {

    } else {
      this.setData({
        isShowWithDraw: true,
      })
    }
  },
  pushGroupDetail(e) {
    wx.navigateTo({
      url: '../mygroup_detail/mygroup_detail?tuan_id=' + e.currentTarget.dataset.id + '&g_id=' + e.currentTarget.dataset.g_id
    })
  },
  stop() {
    clearInterval(this.timer);
  },
})